﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.ListBox;

namespace EditFilesNameFormsApp
{
    public partial class Form1 : Form
    {
        protected int index = 0;
        protected string px = null;
        protected string path = null;
        public Form1()
        {
 
            InitializeComponent();
        }


        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = true;//该值确定是否可以选择多个文件
            dialog.Title = "请选择文件夹";
            dialog.Filter = "所有文件(*.*)|*.*";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string[] files = dialog.FileNames;
                listBox1.Items.Clear();
                foreach (string file in files)
                {
                    listBox1.Items.Add(file);
                }
                
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (textBox2.Text == "")
                {
                    throw new Exception("请输入正确是数值");
                }
                int index = int.Parse(textBox2.Text);
                if (index < 0)
                {
                    index = 0;
                }
                this.index = index;
            }
            catch(Exception)
            {
                textBox2.Text = "";
                MessageBox.Show("请输入正确是数值");
                return;
            } 
            
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
           px = textBox1.Text;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(px =="" ){
                MessageBox.Show("请输入前缀");
                return;
            }else if (listBox1.Items.Count<1)
            {
                MessageBox.Show("请选择文件");
                return;
            }
            if (Directory.Exists(path) == false)
            {
                MessageBox.Show("请选择正确得保存目录");
                return;
            }

            int i = int.Parse(textBox2.Text);
            foreach (string item in listBox1.Items)
            {
                Console.WriteLine(item);
                if (File.Exists(item) == false)
                {
                    continue;
                }
                //string filename = Path.GetFileName(item);//文件名  
                string extension = Path.GetExtension(item);//扩展名 
                File.Copy(item, path + Path.DirectorySeparatorChar + this.px + i + extension, true);
                i++;
            }
            DialogResult dr = MessageBox.Show("操作成功,是否打开目录查看文件？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                System.Diagnostics.Process.Start("Explorer.exe", path);
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择保存目录";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this.path = dialog.SelectedPath;

            }
            if (Directory.Exists(this.path))
            {
                textBox3.Text = this.path;
            }
        }
        //初始位置
        protected int newIndex = -1;
        //调换位置
        protected int oldIndex = -1;
        protected bool start = false;
        protected ContextMenuStrip contextMenuStrip = null;
        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            //转换类型，根据xy坐标找到当前鼠标点击的是哪个下标
            if (e.Button == MouseButtons.Left)
            {
                oldIndex = ((ListBox)sender).IndexFromPoint(e.X, e.Y);
               
            }
            
        
            if (e.Button == MouseButtons.Right && listBox1.SelectedIndex>-1)
            {
                
                Console.WriteLine("MouseDown=" + e.Button);
                contextMenuStrip = new ContextMenuStrip();
                contextMenuStrip.Items.Add("移除");
                contextMenuStrip.Show(this, new Point(e.X, e.Y));
                contextMenuStrip.ItemClicked += ContextMenuStrip_ItemClicked;
            }
            else
            {
                if (oldIndex < 0)
                {
                    listBox1.SelectedIndex = -1;
                }
            }
           
            
            
            
        }

        private void ContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
           if(contextMenuStrip.Items[0] == e.ClickedItem)
            {
                Console.WriteLine(listBox1.SelectedItem);
                listBox1.Items.Remove(listBox1.SelectedItem);
            }
        }

        private void listBox1_DragDrop(object sender, DragEventArgs e)
        {
           
  
        }

        private void listBox1_DragOver(object sender, DragEventArgs e)
        {
            //Console.WriteLine("DragOver");
            //int tIndex = ((ListBox)sender).IndexFromPoint(e.X, e.Y);
            //Console.WriteLine("DragOver=" + tIndex);
        }

        private void listBox1_DragEnter(object sender, DragEventArgs e)
        {
           
            e.Effect = DragDropEffects.Move;
        }

        private void listBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (oldIndex>-1)
            {
              
                int tIndex = ((ListBox)sender).IndexFromPoint(e.X, e.Y);
                if (tIndex > -1 && oldIndex != tIndex)
                {
                    newIndex = tIndex;
                    object temp = listBox1.Items[oldIndex];
                    listBox1.Items[oldIndex] = listBox1.Items[newIndex];
                    listBox1.Items[newIndex] = temp;
                    oldIndex = newIndex;
                }
               
            }
          
        }

        private void listBox1_MouseUp(object sender, MouseEventArgs e)
        {
            oldIndex = -1;
        }
    }
}
